package com.example.accompanist

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.accompanist.ui.theme.ComposeTheme
import com.fanketly.accompanist.banner.Banner3D
import com.fanketly.accompanist.global_popup.GlobalBottomLayoutViewModel
import com.fanketly.accompanist.global_popup.GlobalBottomSheetLayout


class MainActivity : ComponentActivity() {

    @OptIn(ExperimentalMaterialApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeTheme {
                val viewModel: GlobalBottomLayoutViewModel = viewModel()
                GlobalBottomSheetLayout(viewModel) {
                    val imageBack = ImageBitmap.imageResource(id = R.drawable.back2)
//                    val imageMid = ImageBitmap.imageResource(id = R.drawable.mid2)
                    val imageFore = ImageBitmap.imageResource(id = R.drawable.fore3)
                    Column {
                        val view = LocalView.current
                        val density = LocalDensity.current
                        val dp200 = with(density) {
                            200.dp.roundToPx()
                        }
                        val dp60 = with(density) {
                            60.dp.roundToPx()
                        }
                        Banner3D(
                            R.drawable.mid3,
                            height = 200.dp,
                            contentScale = ContentScale.Crop,
                            maxOffset = 20,
                            mMaxAngle = 20,
                            onDraw = { xDistance, yDistance ->
                                translate(-xDistance, -yDistance) {
                                    drawImage(
                                        imageBack,
                                        dstSize = IntSize(
                                            view.width, dp200
                                        ),
                                    )
                                }
                                drawContent()
                                translate(xDistance, yDistance) {
                                    drawImage(
                                        imageFore,
//                                        srcSize = IntSize(400, 400),
                                        dstSize = IntSize(300, 300),
                                        dstOffset = IntOffset(
                                            dp60,
                                            dp60
                                        )
//                                        dstSize = IntSize(foreWidthProportion, foreHeightProportion),
                                    )
                                }
                            }
                        )
                        Spacer(modifier = Modifier.fillMaxHeight(0.3f))
                        Banner3D(
                            backImg = R.drawable.background,
                            midImg = R.drawable.mid,
                            foreImg = R.drawable.fore,
                            height = 200.dp
                        )
//                        Calendar(onDateSelected = { year, month, dayOfMonth ->
//                            viewModel.addSheetContent {
//                                Column {
//                                    Text(
//                                        text = "$month$dayOfMonth 添加计划",
//                                        style = MaterialTheme.typography.h4
//                                    )
//                                    OutlinedTextField(
//                                        value = "",
//                                        onValueChange = {},
//                                        modifier = Modifier.height(200.dp)
//                                    )
//                                    CommonButton(
//                                        text = "添加",
//                                        modifier = Modifier.size(50.dp),
//                                        onClick = { viewModel.removeSheetContent() })
//                                }
//                            }
//                        })
                    }
                }
            }
        }
    }

    @Composable
    fun CommonButton(text: String, modifier: Modifier = Modifier, onClick: () -> Unit) {
        Button(onClick = onClick) {
            Text(text = text, modifier = modifier.size(width = 300.dp, height = 50.dp))
        }
    }
}


